package com.denso.store.admin.template;

import com.denso.store.common.model.Template;
import com.denso.store.common.model.TemplateGroup;
import com.jfinal.core.Controller;

import java.util.List;

/**
 * Created by lijian on 2017/10/24.
 */
public class TemplateController extends Controller {

    static TemplateService service = new TemplateService();

    public void index() {
        int groupId = getParaToInt("gid", 0);
        if(groupId == 0) {
            groupId = getAttrForInt("gid");
        }

        TemplateGroup group = TemplateGroup.dao.findById(groupId);
        setAttr("group", group);

        List<Template> templateList = service.findByGroupId(groupId);
        setAttr("templateList", templateList);
        render("index.html");
    }

    public void add() {
        setAttr("gid", getPara("gid"));
        render("add.html");
    }

    public void save() {
        Template template = getModel(Template.class);
        service.save(template);
        setAttr("notice", "添加成功");
        setAttr("gid", template.getGroupId());
        index();
    }

    public void edit() {
        int id = getParaToInt("id");
        Template template = service.findById(id);
        setAttr("template", template);
        render("edit.html");
    }

    public void update() {
        Template template = getModel(Template.class);
        service.update(template);
        setAttr("notice", "修改成功");
        setAttr("gid", template.getGroupId());
        index();
    }

    public void delete() {
        int id = getParaToInt("id");
        int gid = getParaToInt("gid");
        service.deleteById(id);
        setAttr("notice", "删除成功");
        setAttr("gid", gid);
        index();
    }

    public void activeTemplate() {
        String dateStr = getPara("dateStr");
        int gid = getParaToInt("gid");
        int count = service.activeTemplate(gid, dateStr);

        setAttr("notice", "[" + count + "] 条计划启用成功");
        setAttr("gid", gid);
        index();
    }
}
